#!/bin/bash

# IntelliMark Development Setup Script
echo "🚀 Setting up IntelliMark development environment..."

# Check if Node.js is installed
if ! command -v node &> /dev/null; then
    echo "❌ Node.js is not installed. Please install Node.js 18+ first."
    echo "Visit: https://nodejs.org/"
    exit 1
fi

# Check Node.js version
NODE_VERSION=$(node -v | cut -d'v' -f2)
REQUIRED_VERSION="18.0.0"

if ! node -e "process.exit(require('semver').gte('$NODE_VERSION', '$REQUIRED_VERSION') ? 0 : 1)" 2>/dev/null; then
    echo "❌ Node.js version $NODE_VERSION is too old. Please install Node.js 18+"
    exit 1
fi

echo "✅ Node.js version $NODE_VERSION detected"

# Install dependencies
echo "📦 Installing dependencies..."
if command -v yarn &> /dev/null; then
    yarn install
elif command -v pnpm &> /dev/null; then
    pnpm install
else
    npm install
fi

if [ $? -eq 0 ]; then
    echo "✅ Dependencies installed successfully"
else
    echo "❌ Failed to install dependencies"
    exit 1
fi

# Create necessary directories
echo "📁 Creating project directories..."
mkdir -p assets
mkdir -p docs
mkdir -p scripts

echo "✅ Project structure created"

# Check if .env file exists
if [ ! -f .env ]; then
    echo "📝 Creating example .env file..."
    cat > .env << EOF
# AI Configuration (Optional)
# OpenAI API Key
OPENAI_API_KEY=your_openai_api_key_here

# Anthropic API Key
ANTHROPIC_API_KEY=your_anthropic_api_key_here

# Development
NODE_ENV=development
EOF
    echo "✅ .env file created. Please edit it with your API keys."
fi

echo ""
echo "🎉 Setup complete! You can now start the development server:"
echo ""
echo "   npm run dev"
echo ""
echo "📚 For more information, see README.md"
echo ""
echo "💡 Tip: Add your AI API keys to the .env file to enable AI features!"
echo ""